/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cardarus.operatingsystem.struts.actions;

import com.cardarus.operatingsystem.domain.Customer;
import com.cardarus.operatingsystem.domain.Item;
import com.cardarus.operatingsystem.domain.SalesTransaction;
import com.cardarus.operatingsystem.domain.SalesTransactionRow;
import com.cardarus.operatingsystem.model.BusinessModel;
import com.cardarus.operatingsystem.struts.forms.CheckoutForm;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * Adds a new row in the current SalesTransaction.
 * This  action is called from the Struts framework.
 * 
 * @author Javier Snaider
 */
public class AddCheckoutRowAction extends org.apache.struts.action.Action {
    /**
     *
     */
    public static final double TAX_RATE = 0.1;
   
    /**
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ActionErrors errors = new ActionErrors();
        CheckoutForm cform = (CheckoutForm)form;
        BusinessModel model = BusinessModel.getInstance();
        HttpSession session = request.getSession();
        Customer customer=null;


        SalesTransaction sale = (SalesTransaction) session.getAttribute("sale");

        if (sale == null){
            sale = new SalesTransaction();
            sale.setRows(new ArrayList<SalesTransactionRow>());
            sale.setDate(new Date());
            session.setAttribute("sale", sale);

            customer = (Customer) session.getAttribute("customer");
            if (customer!=null){
                sale.setCustomer(customer);
            }
        }
        List<SalesTransactionRow> rows = sale.getRows();

        Item item = model.getItem(cform.getItemId());
        if (item ==null){
            errors.add (ActionMessages.GLOBAL_MESSAGE,new ActionMessage("errors.checkout.itemnotfound"));
            addErrors(request, errors);
            return mapping.findForward("success");
        }

        double price = item.getPrice()*cform.getQuantity();
        SalesTransactionRow row = new SalesTransactionRow(0,rows.size(),item,cform.getQuantity(),item.getPrice(),price);
        rows.add(row);

        sale.setSubTotal(sale.getSubTotal()+price);
        model.calculateTotal(sale);
        
        return mapping.findForward("success");
    }
}
